package com.iretailer.report.web.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReDevice;
import com.iretailer.report.model.Device;
import com.iretailer.report.model.DeviceMall;
import com.iretailer.report.model.User;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/1/25 11:09
 * @Description:
 */
@RestController
public class DeviceController extends BaseAction {


    @RequestMapping("/business/userDeviceGrid.action")
    public Map<String, Object> userDeviceGrid(@RequestBody ReDevice reDevice, HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        QueryOption queryOption = modelMapper.map(reDevice, QueryOption.class);
        String userId = user.getId();
        if (user.getRoleId().equals("-")) {
            userId = null;
        }
        PageResult<DeviceMall> result = devicesService.grid(userId, reDevice.getEnabled(),reDevice.getDeviceStatus(), reDevice.getDeviceTypeName(), reDevice.getMallName(), reDevice.getDeviceId(), queryOption);
        return RMap.ok(result);

    }

    @RequestMapping("/business/userDeviceStatus.action")
    public Map<String, Object> userDeviceStatus(@RequestBody ReDevice reDevice,HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        QueryOption queryOption = modelMapper.map(reDevice, QueryOption.class);
        String userId = user.getId();
        if (user.getRoleId().equals("-")) {
            userId = null;
        }
        PageResult<DeviceMall> result = devicesService.grid(userId, 1,reDevice.getDeviceStatus(), reDevice.getDeviceTypeName(), reDevice.getMallName(), reDevice.getDeviceId(), queryOption);
        return RMap.ok(result);
    }

    @RequestMapping("/business/saveOrUpdateDevice.action")
    public Map<String, Object> saveOrUpdateDevice(@RequestBody ReDevice reDevice) {
        Device device = modelMapper.map(reDevice, Device.class);
        Device findDevice = devicesService.getOne(new QueryWrapper<Device>().lambda().eq(Device::getDeviceId, device.getDeviceId()));
        if (findDevice != null) {
            if (device.getId() != null && device.getId() <= 0) {
                return RMap.error(RMap.Code.DATAREPETITION, "deviceid_can_not_be_repeated");
            } else {
                if (!findDevice.getId().equals(device.getId())) {
                    return RMap.error(RMap.Code.DATAREPETITION, "deviceid_can_not_be_repeated");
                }
            }
        }
        boolean result = devicesService.saveOrUpdateCamera(device);
        return RMap.resultBoolean(result);
    }


    @RequestMapping("/business/delDevice.action")
    public Map<String, Object> delDevice(@RequestBody List<Long> ids) {
        boolean result = devicesService.deleteDeviceById(ids);
        return RMap.resultBoolean(result);
    }
}
